package view
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.external.ExternalInterface;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	
	import models.ModelLocator;
	
	public class LoadingSprite extends Sprite
	{
		private var loader:Loader;
		private var urlLoader:URLLoader;
		private var urlLoadersize:URLLoader;
		private var url:String;
		private var info:String;
		
		private var txt:TextField;
		private var model:ModelLocator = ModelLocator.getInstance();
		public function LoadingSprite()
		{
			init();
			urlLoader = new URLLoader();
			urlLoader.addEventListener(Event.COMPLETE,onConfig);
			urlLoader.addEventListener(ProgressEvent.PROGRESS,onProgress);
			urlLoader.addEventListener(IOErrorEvent.IO_ERROR,ioError);
			info = "加载配置 ";
			urlLoader.load(new URLRequest("assets/config/config.xml"));
		}
		private function init():void{
			txt = new TextField();
			txt.x = 360;
			txt.y = 230;
			txt.width = 150;
			txt.height = 25;
			txt.textColor = 0xaa503c;
			this.addChild(txt);
			txt.selectable = false;
		}
		private function onConfig(event:Event):void{
			//txt.htmlText = "";
			var xml:XML = XML(event.target.data);
			var obj:Object = new Object();
			obj.size = String(xml.size.@url);
			obj.type = String(xml.type.@url);
			obj.submit = String(xml.submit.@url);
			model.config = obj;
			loadPic();
			
		}
		private function loadPic():void{
			loader = new Loader();
			
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,onProgress);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,ioError);
			info = "加载图片 ";
			var url:String = ExternalInterface.call("getImageURL");
			if(url == null){
				url = "assets/temp/122.jpg";
			}
			loader.load(new URLRequest(url));
			
		}
		private function onComplete(event:Event):void{
			var bitmap:Bitmap = event.target.content;
			model.bitmap = bitmap;
			loadsize();
		}
		private function loadsize():void{
			urlLoadersize = new URLLoader();
			urlLoadersize.addEventListener(Event.COMPLETE,onSizeCom);
			urlLoadersize.addEventListener(ProgressEvent.PROGRESS,onProgress);
			urlLoadersize.addEventListener(IOErrorEvent.IO_ERROR,ioError);
			info = "加载配置图框配置 ";
			urlLoadersize.load(new URLRequest(model.config.size));
		}
		private function onSizeCom(event:Event):void{
			var xml:XML = XML(event.target.data);
			model.setSizeAry(xml);
			this.dispatchEvent(new Event("loadcom"));
		}
		private function onProgress(event:ProgressEvent):void{
			txt.htmlText = "<p align='center'>"+ info + int(event.bytesLoaded/event.bytesTotal*100) +"%</p>"
		}
		private function ioError(evnet:IOErrorEvent):void{
			trace("ioerror");
		}
	}
}